import numpy as np
from scipy.spatial import ConvexHull

def read_points_from_file(file_path):
    points = []
    with open(file_path, 'r') as file:
        for line in file:
            x, y = map(int, line.split())
            points.append([x, y])
    return np.array(points)

if __name__ == "__main__":
    file_path = "random_points_100.txt"

    # 从文件中读取点
    points = read_points_from_file(file_path)

    # 计算凸包
    hull = ConvexHull(points)

    # 输出凸包的点坐标
    print("Convex Hull Points:")
    for vertex in hull.vertices:
        print(tuple(points[vertex]))

    # 绘制凸包和原始点
    import matplotlib.pyplot as plt

    plt.figure(figsize=(20, 16))  # 设置图的大小
    plt.plot(points[:, 0], points[:, 1], 'o', label='Original Points')
    plt.plot(points[hull.vertices, 0], points[hull.vertices, 1], 'r--', label='Convex Hull')

    for i, (xi, yi) in enumerate(points):
        plt.annotate(f'({xi}, {yi})', (xi, yi), textcoords="offset points", xytext=(0, 5), ha='center')


    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title('Convex Hull of a Point Set')
    plt.legend()
    plt.grid(True)
    plt.show()
